//
//  ReservationTimePickerView.swift
//  CMB250716
//
//  Created by Assistant on 2025-01-02.
//

import SwiftUI

/// 预订时间选择器视图
struct ReservationTimePickerView: View {
    @Binding var selectedTime: Date?
    @Binding var isPresented: Bool
    let onConfirm: (Date) -> Void
    @EnvironmentObject var dataManager: DataManager
    
    @State private var availableTimes: [Date] = []
    @State private var selectedTimeIndex: Int = 0
    
    var body: some View {
        NavigationView {
            VStack(spacing: 20) {
                // 标题和说明
                VStack(spacing: 10) {
                    Text(dataManager.currentLanguage == "zh" ? "选择预订时间" : "Select Reservation Time")
                        .font(.title2)
                        .fontWeight(.bold)
                        .foregroundColor(.white)
                    
                    Text(dataManager.currentLanguage == "zh" ? 
                         "现在是非营业时间，请选择预订时间" : 
                         "We're currently closed. Please select a reservation time")
                        .font(.subheadline)
                        .foregroundColor(.gray)
                        .multilineTextAlignment(.center)
                        .padding(.horizontal)
                }
                
                if availableTimes.isEmpty {
                    VStack(spacing: 15) {
                        Image(systemName: "clock.badge.exclamationmark")
                            .font(.system(size: 50))
                            .foregroundColor(.orange)
                        
                        Text(dataManager.currentLanguage == "zh" ? 
                             "暂无可预订时间" : 
                             "No available reservation times")
                            .font(.headline)
                            .foregroundColor(.white)
                    }
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
                } else {
                    // 时间选择器
                    VStack(spacing: 15) {
                        Text(dataManager.currentLanguage == "zh" ? "可预订时间" : "Available Times")
                            .font(.headline)
                            .foregroundColor(.white)
                        
                        Picker("预订时间", selection: $selectedTimeIndex) {
                            ForEach(0..<availableTimes.count, id: \.self) { index in
                                Text(BusinessHoursManager.formatReservationTime(availableTimes[index]))
                                    .foregroundColor(.white)
                                    .tag(index)
                            }
                        }
                        .pickerStyle(WheelPickerStyle())
                        .frame(height: 200)
                        .background(
                            RoundedRectangle(cornerRadius: 12)
                                .fill(Color.black.opacity(0.3))
                        )
                    }
                    
                    Spacer()
                    
                    // 确认按钮
                    Button(action: {
                        if selectedTimeIndex < availableTimes.count {
                            let selectedDate = availableTimes[selectedTimeIndex]
                            selectedTime = selectedDate
                            onConfirm(selectedDate)
                            isPresented = false
                        }
                    }) {
                        HStack {
                            Image(systemName: "checkmark.circle.fill")
                            Text(dataManager.currentLanguage == "zh" ? "确认预订" : "Confirm Reservation")
                        }
                        .font(.headline)
                        .foregroundColor(.white)
                        .frame(maxWidth: .infinity)
                        .frame(height: 50)
                        .background(
                            LinearGradient(
                                colors: [Color.orange, Color.red],
                                startPoint: .leading,
                                endPoint: .trailing
                            )
                        )
                        .cornerRadius(25)
                    }
                    .disabled(availableTimes.isEmpty)
                    .padding(.horizontal)
                }
            }
            .padding()
            .background(
                LinearGradient(
                    colors: [
                        Color(red: 0.12, green: 0.12, blue: 0.12),
                        Color(red: 0.18, green: 0.16, blue: 0.15)
                    ],
                    startPoint: .topLeading,
                    endPoint: .bottomTrailing
                )
                .ignoresSafeArea()
            )
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) {
                    Button(dataManager.currentLanguage == "zh" ? "取消" : "Cancel") {
                        isPresented = false
                    }
                    .foregroundColor(.orange)
                }
            }
        }
        .onAppear {
            loadAvailableTimes()
        }
    }
    
    private func loadAvailableTimes() {
        availableTimes = BusinessHoursManager.getAvailableReservationTimes()
        selectedTimeIndex = 0
    }
}

// MARK: - 预览
struct ReservationTimePickerView_Previews: PreviewProvider {
    static var previews: some View {
        ReservationTimePickerView(
            selectedTime: .constant(nil),
            isPresented: .constant(true),
            onConfirm: { _ in }
        )
        .environmentObject(DataManager.shared)
    }
}